package yyy.ab.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.BaseController;
import yyy.ab.common.core.ResultBean;
import yyy.ab.modules.system.entity.SysOrganization;
import yyy.ab.modules.system.service.SysOrganizationService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：机构管理")
@RequestMapping("/system/organization")
public class SysOrganizationController extends BaseController {

    private final SysOrganizationService service;

    /**
     * 机构页面
     */
    @RequestMapping
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/organization/index");
        return view;
    }

    //    @Log("查询机构")
    @ApiOperation("查询机构")
    @GetMapping("/findPage")
    public Object query(@RequestParam Map<String, Object> params) {
        IPage<SysOrganization> page = service.findPage(params);
        return jsonForTable(page);
    }

    @ApiOperation("查询机构列表")
    @GetMapping("/findList")
    public Object queryList() {
        QueryWrapper<SysOrganization> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<SysOrganization> list = service.list(queryWrapper);
        return list;
    }


    @ApiOperation("查询单个机构")
    @GetMapping("/get")
    public SysOrganization get(Long id) {
        SysOrganization dept = service.getById(id);
        return dept;
    }

    @ApiOperation("创建机构")
    @PostMapping
    public Object create(@RequestBody SysOrganization dept) {
        ResultBean resultBean = service.create(dept);
        return resultBean;
    }

    @ApiOperation("修改机构")
    @PutMapping
    public Object update(@RequestBody SysOrganization dept) {
        ResultBean resultBean = service.update(dept);
        return resultBean;
    }

    @ApiOperation("删除机构")
    @DeleteMapping
    public Object deleteAll(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        ResultBean resultBean = service.deleteAll(idList);
        return resultBean;
    }
}
